<template>
  <div class="rank-list"  >
    <div class="rank-head">
      <div class="rank-title">
        {{$t('carGame.rank_title')}}
      </div>
      <div v-if="!isActivityEnd" class="rank-cd">
        <span>{{$t('carGame.deadline_title')}}</span>
        <span><span class="c-gold">{{cdData.days}}</span> {{$t('carGame.day')}} <span class="c-gold">{{cdData.hours}}</span> {{$t('carGame.hour')}} <span class="c-gold">{{cdData.minutes}}</span> {{$t('carGame.minute')}} <span class="c-gold">{{cdData.seconds}}</span> {{$t('carGame.second')}}</span>
        <span @click="toggleShow" class="flod-tag">
          <span v-text="showList?$t('carGame.pack_up'):$t('carGame.expand')"></span>
          <span v-bind:class="{'allow-up': showList}" class="allow-icon"></span>
        </span>
      </div>
      <div v-if="isActivityEnd" class="rank-cd" style="padding: 0;">
        <span>{{$t('carGame.rank_stop')}}</span>
      </div>
    </div>
    <div v-show="showList" class="rank-body">
      <div class="rank-line rank-body-head">
        <span>{{$t('carGame.rank')}}</span>
        <span>{{$t('carGame.user_name')}}</span>
        <span>{{$t('carGame.bet_amount')}}</span>
        <span>{{$t('carGame.award')}}</span>
      </div>
      <div v-for="(item, index) in rankList" v-bind:key="index" class="rank-line">
        <span v-bind:class="{'rank-first': index === 0, 'rank-second': index === 1, 'rank-third': index === 2}" v-text="index>2?index+1: ''"> </span>
        <span @click="toEosPark(item.user)">{{item.user}}</span>
        <span>{{item.bets | amountToEos()}}</span>
        <span>{{sortAmount[index]}}</span>
      </div>
      <!--<div class="rank-line">-->
        <!--<span class="rank-second"> </span>-->
        <!--<span>zizizzizi5</span>-->
        <!--<span>150 EOS</span>-->
        <!--<span>1.5 EOS</span>-->
      <!--</div>-->
      <!--<div class="rank-line">-->
        <!--<span class="rank-third"> </span>-->
        <!--<span>zizizzizi5</span>-->
        <!--<span>150 EOS</span>-->
        <!--<span>1.5 EOS</span>-->
      <!--</div>-->
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      showList: true,
      rankList: [],
      sortAmount: [
        '1200 EOS', '800 EOS', '500 EOS', '200 EOS', '100 EOS'
      ],
      cdData: {
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00'
      },
      rankCD: null,
      isActivityEnd: false
    }
  },
  created () {
    this.getRankList()
  },
  computed: {
    difTime () {
      return this.$store.getters.getDifTime
    }
  },
  watch: {
    difTime (val) {
      this.initCD(val)
    }
  },
  methods: {
    toggleShow () {
      this.showList = !this.showList
    },
    toEosPark (user) {
      window.location.href = 'https://eospark.com/account/' + user
    },
    initCD (difTime) {
      if (difTime) {
        this.clearRankCD()
        this.rankCD = setInterval(() => {
          this.calcCDdata(difTime)
        }, 1000)
        this.calcCDdata(difTime)
      }
    },
    clearRankCD () {
      if (this.rankCD) {
        clearInterval(this.rankCD)
        this.rankCD = null
      }
    },
    calcCDdata (difTime) {
      var endTime = this.$config.rankTimestamp * 1000
      var curServiceTime = new Date().getTime() - difTime
      if (curServiceTime >= endTime) {
        this.cdData.days = '00'
        this.cdData.hours = '00'
        this.cdData.minutes = '00'
        this.cdData.seconds = '00'
        this.isActivityEnd = true
        this.clearRankCD()
      } else {
        var remainTime = (endTime - curServiceTime) / 1000
        var days = parseInt(remainTime / (3600 * 24))
        var hours = parseInt((remainTime % (3600 * 24)) / 3600)
        var minutes = parseInt((remainTime % 3600) / 60)
        var seconds = parseInt(remainTime % 60)
        this.cdData.days = days >= 10 ? days : '0' + days
        this.cdData.hours = hours >= 10 ? hours : '0' + hours
        this.cdData.minutes = minutes >= 10 ? minutes : '0' + minutes
        this.cdData.seconds = seconds >= 10 ? seconds : '0' + seconds
      }
    },
    getRankList () {
      var myDate = new Date()
      if (myDate.getTime() >= this.$config.rankTimestamp * 1000) {
        myDate = new Date(this.$config.rankTimestamp * 1000)
      }
      var month = myDate.getMonth() + 1
      month = month < 10 ? '0' + month : month
      var curDate = myDate.getDate()
      curDate = curDate < 10 ? '0' + curDate : curDate
      var date = myDate.getFullYear() + '' + month + curDate

      this.gameMongodb('getBetSort', {date: date}, data => {
        var tmpData = data.data.sort((item1, item2) => {
          return item2.bets - item1.bets
        })
        var showRankList = []
        var unshowUser = ['eoseoseosyyy', 'xinggege1234', 'chendeng1234']
        for (let i = 0; i < tmpData.length; i++) {
          if (unshowUser.indexOf(tmpData[i].user) === -1) {
            showRankList.push(tmpData[i])
            if (showRankList.length === 5) {
              break
            }
          }
        }
        this.rankList = showRankList
        setTimeout(() => {
          this.getRankList()
        }, 60 * 1000)
      })
    },
    gameMongodb (url, json, callback) {
      const axios = require('axios')
      axios.post(this.$config.gameMongodbApiUrl + '/cargame/' + url, json).then(function (response) {
        callback(response.data)
      }).catch(function (error) {
        console.log(error)
      })
    }
  }
}
</script>

<style scoped>
  .rank-list {
    padding: 0 10px;
    max-width: 1220px;
    margin: auto;
    color: #fff;
  }
  .rank-head {
    text-align: center;
    background-color: #112850;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    padding-bottom: 5px;
  }
  .rank-title {
    font-size: 15px;
    color: #e7b425;
    line-height: 36px;
  }
  .rank-cd {
    position: relative;
    padding-right: 20px;
    font-size: 15px;
  }
  .rank-cd .c-gold {color: #02bf01;}
  .flod-tag {
    position: absolute;
    right: 0;
    padding: 0 5px;
    top: 2px;
    vertical-align: middle;
    line-height: 0;
  }
  .flod-tag>span {
    display: inline-block;
    vertical-align: middle;
  }
  .allow-icon {
    display: inline-block;
    height: 16px;
    width: 16px;
    background-image: url('../assets/image/allowdown.png');
    background-size: 100% 100%;
    vertical-align: middle;
  }
  .allow-up {
    transform: rotate(180deg);
  }
  .rank-body {
    border-top: 6px solid #1e3c72;
    background-color: #112850;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    text-align: center;
    max-height: 297px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .rank-body>.rank-line:last-of-type {border-bottom: none;}
  .rank-line {
    border-bottom: 1px solid #5e647e;
    line-height: 0;
    font-size: 0;
  }
  .rank-line>span {
    display: inline-block;
    line-height: 50px;
    height: 50px;
    vertical-align: middle;
    font-size: 14px;
  }
  .rank-body-head>span {line-height: 36px;height: 36px;}
  .rank-line>span:nth-of-type(1) {
    width: 16%;
  }
  .rank-line>span:nth-of-type(2) {
    width: 26%;
  }
  .rank-line>span:nth-of-type(3) {
    width: 32%;
  }
  .rank-line>span:nth-of-type(4) {
    width: 26%;
  }
  .rank-first {
    background-image: url("../assets/image/rank1.png");
    background-position: center 8px;
    background-size: 40px 36px;
    background-repeat: no-repeat;
  }
  .rank-second {
    background-image: url("../assets/image/rank2.png");
    background-position: center 8px;
    background-size: 40px 36px;
    background-repeat: no-repeat;
  }
  .rank-third {
    background-image: url("../assets/image/rank3.png");
    background-position: center 8px;
    background-size: 40px 36px;
    background-repeat: no-repeat;
  }
</style>
